<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="js/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/iconfont.css">
    <link rel="stylesheet" href="css/main.css">
    <link type="text/css" rel="stylesheet" href="js/jedate/css/jedate.css">
    <script src="js/lib/jquery-1.12.4.min.js"></script>
    <script src="js/jedate/js/jedate.js"></script>
    <script src="js/tinymce/js/tinymce/tinymce.min.js"></script>
    <script src="js/tinymce/js/tinymce_setup.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章编辑
        </div>
        <div class="container-fluid common_con">
            <form class="form-horizontal article_form" id="myform">
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">文章标题：</label>
                    <div class="col-sm-10">
                        <input type="text" name="title" class="form-control" id="article_title" value="文章标题文字" />
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputPassword3" class="col-sm-2 control-label">文章封面：</label>
                    <div class="col-sm-10">
                        <img src="images/pic06.jpg" class="article_cover" id="article_cover" />
                        <input type="file" id="exampleInputFile" name="cover" />
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">文章类别：</label>
                    <div class="col-sm-4">
                        <select class="form-control" name="type" id="article_type"> </select>
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">发布时间：</label>
                    <div class="col-sm-4">
                        <div class="input-group">
                            <input type="text" class="form-control" name="date" value="2019-05-17" id="dateinput" />
                            <span class="input-group-btn">
                                <button class="btn btn-default" type="button"
                                    onclick="jeDate('#dateinput',{trigger:false,format: 'YYYY-MM-DD'})"><i
                                        class="iconfont icon-icondate"></i></button>
                            </span>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">文章内容：</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="rich_content" name="content" placeholder="文章内容"
                            value="文章内容" />
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="button" id="btnSave" class="btn btn-success">修改</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <script src="./js/utils/config.js"></script>
    <script src="./js/utils/article.js"></script>
    <script src="./js/utils/category.js"></script>
    <script src="./js/lib/template-web.js"></script>
    <script id="tmp" type="text/html">
        {{each data item}}
        <option value="{{item.id}}">{{item.name}}</option>
        {{/each}}
    </script>

    <script>
        // id=282&a=1&b=2 ===> {id:282,a:1,b:2}
        // 查询字符串，转对象
        function formatToObj(str) {
            var obj = {};
            // 用&进行拆分
            var arr1 = str.split('&');
            // arr1 = ["id=282","a=1","b=2"]
            arr1.forEach(function (item) {
                var arr2 = item.split('=');
                obj[arr2[0]] = arr2[1];
            });
            return obj;
        }

        $(function () {
            // 把？去掉
            var str = window.location.search.replace('?', '');
            var obj = formatToObj(str);
            var id = obj.id;
            //   console.log(id);
            // 1. 先获取文章的信息，显示出来
            //    1）从地址栏中取出id值来 2）发请求
            getAndShow(id);
            // 2. 用户修改
            $('#btnSave').click(function () {
                //保存文章
                // 1)取数据
                var fd = new FormData($('#myform')[0]);
                // todos: 对数据进行有效性的判断
                fd.append('id', id);
                fd.set('content',tinyMCE.activeEditor.getContent());

                article.edit(fd,function(res) {
                    if(res.code === 200) {
                        // todos: 美化
                        alert('修改成功');
                    }else {
                        alert('修改失败');
                    }
                });
            });

        function getAndShow(id) {
            //如果id不是数值
            if (isNaN(id)) {
                alert('id不对');
            } else {
                // 获取详情并显示   
                article.show({
                    id: id
                }, function (res) {
                    if (res.code === 200) {
                        // 去获取 文章类别
                        category.show(function (res) {
                            if (res.code === 200) {
                                var htmlStr = template('tmp', res);
                                $('#article_type').html(htmlStr);
                                $('#article_type').val(res.data.type);
                            }
                        });
                        $('#article_title').val(res.data.title);
                        $('#article_cover').attr('src', res.data.cover);
                        $('#dateinput').val(res.data.date);
                        setTimeout(function () {
                            // 如果不设置延迟，可能会报错。报错的原因tinyMCE还没有准备就绪，就给它设置了内容
                            tinyMCE.activeEditor.setContent(res.data.content);
                        }, 1000);
                    }
                });
            }
        }
    });
    </script>

</body>

</html>